<template>
  <div>
    <h3>报销记录管理</h3>

    <!-- 查询功能 -->
    <el-input v-model="searchQuery" placeholder="请输入服务名称进行查询" style="width: 300px; margin-bottom: 20px;" clearable></el-input>
    <el-button type="primary" @click="fetchMedicalServices">查询</el-button>

    <!-- 添加报销记录按钮 -->
    <el-button type="primary" @click="openDialog(null)">添加报销记录</el-button>

    <!-- 报销记录列表 -->
    <el-table :data="reimbursementList" style="width: 100%">
      <el-table-column prop="insuredpersonid" label="参保人员ID" width="180"></el-table-column>
      <el-table-column prop="medicalserviceid" label="医疗服务ID" width="180"></el-table-column>
      <el-table-column prop="reimbursementdate" label="报销日期" width="180"></el-table-column>
      <el-table-column prop="approvedamount" label="报销金额" width="180"></el-table-column>
      <el-table-column prop="personalexpense" label="个人自付" width="180"></el-table-column>
      <el-table-column prop="totalcost" label="总费用" width="180"></el-table-column>
      <el-table-column prop="reimbursementstatus" label="报销状态" width="180"></el-table-column>

      <!-- 操作列 -->
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button size="small" type="primary" @click="openDialog(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteReimbursement(scope.row.insuredpersonid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑报销记录对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible">
      <el-form :model="reimbursementForm" label-width="120px">
        <el-form-item label="参保人员ID">
          <el-input v-model="reimbursementForm.insuredpersonid"></el-input>
        </el-form-item>
        <el-form-item label="医疗服务ID">
          <el-input v-model="reimbursementForm.medicalserviceid"></el-input>
        </el-form-item>
        <el-form-item label="报销日期">
          <el-date-picker v-model="reimbursementForm.reimbursementdate" type="date"  placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="报销金额">
          <el-input v-model="reimbursementForm.approvedamount"></el-input>
        </el-form-item>
        <el-form-item label="个人自付">
          <el-input v-model="reimbursementForm.personalexpense"></el-input>
        </el-form-item>
        <el-form-item label="总费用">
          <el-input v-model="reimbursementForm.totalcost"></el-input>
        </el-form-item>
        <el-form-item label="报销状态">
          <el-input disabled v-model="reimbursementForm.reimbursementstatus"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveReimbursement">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      reimbursementList: [],
      reimbursementForm: {
        insuredpersonid: '',
        medicalserviceid: '',
        reimbursementdate: '',
        approvedamount: '',
        personalexpense: '',
        totalcost: '',
        reimbursementstatus: '0'
      },
      dialogVisible: false,
      dialogTitle: '',
      isEditing: false
    };
  },
  mounted() {
    this.fetchStaffReimbursementRecordList()
  },
  methods: {
    // 获取报销记录列表
    fetchStaffReimbursementRecordList() {
      axios.get('http://localhost:8081/reimbursementRecord/all')
          .then(response => {
            this.reimbursementList = response.data
          })
          .catch(error => {
            console.error("Error fetching insured persons:", error);
          });
    },
    // 打开添加/编辑对话框
    openDialog(row) {
      if (row) {
        this.isEditing = true;
        this.dialogTitle = '编辑报销记录';
        this.reimbursementForm = {...row};
      } else {
        this.isEditing = false;
        this.dialogTitle = '添加报销记录';
        this.resetForm();
      }
      this.dialogVisible = true;
    },
    // 重置表单
    resetForm() {
      this.reimbursementForm = {
        insuredpersonid: '',
        medicalserviceid: '',
        reimbursementdate: '',
        approvedamount: '',
        personalexpense: '',
        totalcost: '',
        reimbursementstatus: '0'
      };
    },
    // 保存报销记录
    saveReimbursement() {
      if (this.isEditing) {
        axios.put(`http://localhost:8081/reimbursementRecord/update`, this.reimbursementForm)
            .then(() => {
              this.fetchStaffReimbursementRecordList();
              this.dialogVisible = false;
            })
            .catch(error => {
              console.error("Error updating insured person:", error);
            });
      } else {
        axios.post('http://localhost:8081/reimbursementRecord/add', this.reimbursementForm)
            .then(() => {
              this.fetchStaffReimbursementRecordList();
              this.dialogVisible = false;
            })
            .catch(error => {
              console.error("Error adding insured person:", error);
            });
      }
    },
    // 删除报销记录
    deleteReimbursement(insuredpersonid) {
      axios.delete(`http://localhost:8081/reimbursementRecord/delete/${insuredpersonid}`)
          .then(() => {
            this.fetchStaffReimbursementRecordList();
          })
          .catch(error => {
            console.error("Error deleting insured person:", error);
          });
    }
  }
};
</script>

<style scoped>
.el-button {
  margin-bottom: 20px;
}
.dialog-footer {
  text-align: right;
}
</style>
